import conf from './config'
import axios from 'axios'
import {Message} from 'element-ui'

let qs = require('qs')

let api = conf.api

let Axios = axios.create()

/**
 * @intro 请求拦截器
 */
Axios.interceptors.request.use(
  config => {
    // let AUTH_TOKEN = (function() {
    //   return localStorage.getItem('token')
    // }())
    // if(AUTH_TOKEN){
    //   config.headers.Authorization = AUTH_TOKEN
    // }
    config.headers['Content-Type'] = 'application/x-www-form-urlencoded';
    return config
  },
  error => {
    console.log(error)
  }
)

/**
 * @intro 响应拦截器
 */
Axios.interceptors.response.use(response =>{
  if(response.status === 200 && response.data.status === 200){
    return response.data.result
  }else{
    Message.warning(response.data.message ? response.data.message : '网络连接失败')
    return false
  }
}, err => {
  Message.warning('请求超时')
})

export const axget = (url = '/index',params = {}) => {
    return Axios.get(`${api}${url}`,{params:params})
}
export const axpost = (url = '/index',params = {}) => {
    return Axios.post(`${api}${url}`,qs.stringify(params))
}
